﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace ListItemEditor
{
    public partial class UploadProgressForm : Form
    {
        public string DestinationFolder { get; set; }
        public string[] SourcePaths { get; set; }
        public ICredentials Credentials { get; set; }

        private bool _hasErrors;
        private MultipleUploadFileHelper _uploadHelper;

        public UploadProgressForm()
        {
            InitializeComponent();
        }

        private void UploadProgressForm_Load(object sender, EventArgs e)
        {
            if (this.SourcePaths == null || this.SourcePaths.Length == 0) return;

            this.btnOK.Enabled = false;
            progressBar.Minimum = 0;
            progressBar.Maximum = this.SourcePaths.Length;

            _uploadHelper = new MultipleUploadFileHelper();
            _uploadHelper.FileUploading += new EventHandler<ProgressEventArgs>(_uploadHelper_FileUploading);
            _uploadHelper.FileUploaded += new EventHandler<ProgressEventArgs>(_uploadHelper_FileUploaded);

            _uploadHelper.UploadMultipleFiles(this.Credentials, this.DestinationFolder, this.SourcePaths);
        }

        protected void _uploadHelper_FileUploading(object sender, ProgressEventArgs e)
        {
            Delegate dlg = new MethodInvoker(() =>
                {
                    lblStatus.Text = "Uploading " + e.FileName;

                });
            this.Invoke(dlg);
        }

        protected void _uploadHelper_FileUploaded(object sender, ProgressEventArgs e)
        {
            Delegate dlg = new MethodInvoker(() =>
            {
                if (e.TotalItems > progressBar.Maximum) progressBar.Maximum = e.TotalItems;

                lblStatus.Text = "Uploaded " + e.FileName;
                this.progressBar.Value = e.CurrentItem + 1;

                string msg = Path.GetFileName(e.FileName);
                if (e.Success) msg += " - OK";
                else
                {
                    msg += " - " + e.ErrorMessage;
                    this._hasErrors = true;
                }
                int idx = this.lbFiles.Items.Add(msg);
                this.lbFiles.SelectedIndex = idx;

                if (e.CurrentItem >= e.TotalItems - 1)
                {
                    if (_hasErrors) this.lblStatus.Text = "There were errors while uploading the files.";
                    else this.lblStatus.Text = "Files uploaded successfully.";
                    this.btnOK.Enabled = true;
                }
            });

            this.Invoke(dlg);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void UploadProgressForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = this.DialogResult != DialogResult.OK;
        }

        
    }
}
